package groupb6.shopsystem.service.impl;

import groupb6.shopsystem.mapper.DestinationMapper;
import groupb6.shopsystem.mapper.StoreMapper;
import groupb6.shopsystem.mapper.UserMapper;
import groupb6.shopsystem.pojo.entity.Destination;
import groupb6.shopsystem.pojo.entity.Store;
import groupb6.shopsystem.pojo.entity.User;
import groupb6.shopsystem.pojo.form.LoginForm;
import groupb6.shopsystem.pojo.form.RegisterNormalForm;
import groupb6.shopsystem.pojo.form.RegisterSellerForm;
import groupb6.shopsystem.pojo.res.TokenRes;
import groupb6.shopsystem.util.JWTUtil;
import groupb6.shopsystem.util.enums.IsDefaultDestEnum;
import groupb6.shopsystem.util.enums.ResponseEnum;
import groupb6.shopsystem.util.enums.IdentityEnum;
import groupb6.shopsystem.util.enums.StoreStatusEnum;
import groupb6.shopsystem.util.response.ResponseException;
import groupb6.shopsystem.util.response.UniversalResponse;
import groupb6.shopsystem.service.IndexService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private DestinationMapper destinationMapper;
    /**
     * 用户登录
     *
     * @param loginForm 登录表单
     * @return
     */
    @Override
    public UniversalResponse<TokenRes> login(LoginForm loginForm) {
        User user = userMapper.getUserByAccount(loginForm.getAccount());
        if (user == null) {
            throw new ResponseException(ResponseEnum.USER_LOGIN_ERROR.getCode(), ResponseEnum.USER_LOGIN_ERROR.getMsg());
        }
        if (!user.getPassword().equals(loginForm.getPassword())) {
            throw new ResponseException(ResponseEnum.USER_LOGIN_ERROR.getCode(), ResponseEnum.USER_LOGIN_ERROR.getMsg());
        }
        // 若为商家，检查是否审批通过
        if (IdentityEnum.SELLER.getIdentity().equals(user.getIdentity())) {
            Store store = storeMapper.getStoreBySellerId(user.getId());
            if (store == null) {
                throw new ResponseException(ResponseEnum.STORE_NOT_FOUND.getCode(), ResponseEnum.STORE_NOT_FOUND.getMsg());
            }
            if (!StoreStatusEnum.ACCEPTED.getStatus().equals(store.getStatus())) {
                throw new ResponseException(ResponseEnum.STORE_NOT_ACCEPTED.getCode(), ResponseEnum.STORE_NOT_ACCEPTED.getMsg());
            }
        }
        // 生成token
        String token;
        token = JWTUtil.createToken(loginForm.getAccount());
        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), new TokenRes(token, user.getIdentity()));
    }

    /**
     * 用户注册
     *
     * @param registerNormalForm
     * @return
     */
    @Override
    public UniversalResponse<?> register(RegisterNormalForm registerNormalForm) {
        User newUser = new User();
        newUser.setId(null);
        newUser.setName(registerNormalForm.getName());
        newUser.setIdentity(IdentityEnum.BUYER.getIdentity());
        newUser.setAccount(registerNormalForm.getAccount());
        newUser.setPassword(registerNormalForm.getPassword());

        if (Strings.isBlank(newUser.getName()) || Strings.isBlank(newUser.getPassword()) || Strings.isBlank(newUser.getAccount())) {
            throw new ResponseException(ResponseEnum.PARAM_IS_INVALID.getCode(), ResponseEnum.PARAM_IS_INVALID.getMsg());
        }

        User user = userMapper.getUserByAccount(newUser.getAccount());
        if (user != null) {
            throw new ResponseException(ResponseEnum.USER_ACCOUNT_EXISTS.getCode(), ResponseEnum.USER_ACCOUNT_EXISTS.getMsg());
        }

        userMapper.insert(newUser);

        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }

    /**
     * 商家注册
     * @param registerSellerForm
     * @return
     */
    @Override
    public UniversalResponse<?> registerSeller(RegisterSellerForm registerSellerForm) {
        User newUser = new User();
        newUser.setId(null);
        newUser.setName(registerSellerForm.getName());
        newUser.setIdentity(IdentityEnum.SELLER.getIdentity());
        newUser.setAccount(registerSellerForm.getAccount());
        newUser.setPassword(registerSellerForm.getPassword());
        newUser.setTel(registerSellerForm.getConnect());
        newUser.setAvatar(registerSellerForm.getLogoPath());

        if (Strings.isBlank(newUser.getName()) || Strings.isBlank(newUser.getPassword()) || Strings.isBlank(newUser.getAccount())) {
            throw new ResponseException(ResponseEnum.PARAM_IS_INVALID.getCode(), ResponseEnum.PARAM_IS_INVALID.getMsg());
        }

        User user = userMapper.getUserByAccount(newUser.getAccount());
        if (user != null) {
            throw new ResponseException(ResponseEnum.USER_ACCOUNT_EXISTS.getCode(), ResponseEnum.USER_ACCOUNT_EXISTS.getMsg());
        }

        userMapper.insert(newUser);

        User thisUser = userMapper.getUserByAccount(newUser.getAccount());
        Store newStore = new Store();

        newStore.setId(null);
        newStore.setSellerId(thisUser.getId());
        newStore.setStoreName(registerSellerForm.getStore_name());
        newStore.setDetail(registerSellerForm.getDetail());
        newStore.setConnect(registerSellerForm.getConnect());
        newStore.setScope(registerSellerForm.getScope());
        newStore.setLogoPath(registerSellerForm.getLogoPath());
        newStore.setStatus(StoreStatusEnum.NOT_PROCESSED.getStatus());

        if (Strings.isBlank(newStore.getStoreName())
                || Strings.isBlank(newStore.getDetail())
                || Strings.isBlank(newStore.getConnect())
                || Strings.isBlank(newStore.getScope())
                || Strings.isBlank(newStore.getLogoPath())) {
            userMapper.deleteById(thisUser);
            throw new ResponseException(ResponseEnum.PARAM_IS_INVALID.getCode(), ResponseEnum.PARAM_IS_INVALID.getMsg());
        }

        storeMapper.insert(newStore);

        Destination newDestination = new Destination();
        newDestination.setId(null);
        newDestination.setUserId(thisUser.getId());
        newDestination.setName(thisUser.getName());
        newDestination.setPhone(registerSellerForm.getPhone());
        newDestination.setAddress(registerSellerForm.getAddress());
        newDestination.setIsDefault(IsDefaultDestEnum.IS_DEFAULT.getValue());
        if (Strings.isBlank(newDestination.getAddress())
                || Strings.isBlank(newDestination.getPhone())
                || Strings.isBlank(newDestination.getName())) {
            userMapper.deleteById(thisUser);
            storeMapper.deleteById(newStore);
            throw new ResponseException(ResponseEnum.PARAM_IS_INVALID.getCode(), ResponseEnum.PARAM_IS_INVALID.getMsg());
        }

        destinationMapper.insert(newDestination);

        return new UniversalResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }
}
